<template>
  <div>
    <Panel>
      <span slot="title">商品添加</span>
      <div slot="main">
        <!-- 表单 -->
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="商品名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>

          <el-form-item label="商品分类">
            <el-select v-model="form.category" placeholder="请选择商品分类">
              <el-option
                v-for="(v, i) in arr"
                :key="i"
                :label="v"
                :value="v"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="商品价格">
            <el-input-number
              v-model="price"
              @change="handleChange"
              :min="1"
              :max="10"
              label="商品价格"
            ></el-input-number>
          </el-form-item>

          <el-form-item label="商品图片">
            <el-upload
              class="avatar-uploader"
              action="http://sell.h5.itsource.cn:8087/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="img" :src="img" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="商品描述">
            <el-input type="textarea" v-model="form.goodsDesc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">添加商品</el-button>
          </el-form-item>
        </el-form>
      </div>
    </Panel>
  </div>
</template>

<script>
import Panel from "@/components/panel.vue";
import { goriescateApi, addApi } from "@/api/goods";
export default {
  components: {
    Panel,
  },
  data() {
    return {
      form: {
        name: "",
        category: "",
        goodsDesc: "",
      },
      //   价格
      price: 1,

      // 商品分类
      arr: [],

      // 图片上传
      imageUrl: "",
      img: "",
    };
  },
  created() {
    this.assgoods();
  },
  methods: {
    // 获取商品分类
    assgoods() {
      goriescateApi().then((res) => {
        // console.log(res);
        this.arr = res.data;
      });
    },

    // 价格
    handleChange(value) {
      //console.log(value);
    },

    // 图片上传
    handleAvatarSuccess(res, file) {
      //console.log(res);
      this.img = res.imgUrl;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 提交
    async onSubmit() {
      // alert("发ajax");
      let res = await addApi({
        name: this.form.name,
        category: this.form.category,
        price: this.price,
        imgUrl: this.img,
        goodsDesc: this.form.goodsDesc,
      });
      if (res.data.code == 0) {
        this.$router.push("/goods/GoodsList");
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>